package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityDeviceUnitBinding;
import com.apex.aolonfitdemo.dialog.DeviceUnitDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.UnitCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.enumeration.UnitFormat;
import com.apex.ax_bluetooth.model.EABleDevUnit;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;

public class DeviceUnitActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    private DeviceUnitDialog deviceUnitDialog;
    private String tempRest;


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                if (msg.arg1 == 1) {
                    activityDeviceUnitBinding.restScreen.setText(getString(R.string.device_unit_metric));
                } else {
                    activityDeviceUnitBinding.restScreen.setText(getString(R.string.device_unit_imperial));
                }


            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing())
                        waitingDialog.dismiss();
                }

                activityDeviceUnitBinding.restScreen.setText(tempRest + "");
            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(DeviceUnitActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityDeviceUnitBinding activityDeviceUnitBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDeviceUnitBinding = ActivityDeviceUnitBinding.inflate(getLayoutInflater());
        setContentView(activityDeviceUnitBinding.getRoot());
        activityDeviceUnitBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDeviceUnitBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDeviceUnitBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(DeviceUnitActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().setOnMetricImperialListener(new OnCyberQueryAndSetStateListener<Integer>() {
            @Override
            public void queryConfig(Integer integer) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.arg1 = integer;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        AolonFitManager.getInstance().queryMetricImperial();

        activityDeviceUnitBinding.restScreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (deviceUnitDialog == null) {
                    deviceUnitDialog = new DeviceUnitDialog(DeviceUnitActivity.this);
                    deviceUnitDialog.setSelectListener(new DeviceUnitDialog.SelectListener() {

                        @Override
                        public void selectData(String sex) {
                            if (waitingDialog == null) {
                                waitingDialog = new WaitingDialog(DeviceUnitActivity.this);
                            }
                            if (!waitingDialog.isShowing()) {
                                waitingDialog.show();
                            }
                            tempRest = sex;


                            if (sex.equalsIgnoreCase(getString(R.string.device_unit_imperial))) {
                                AolonFitManager.getInstance().setMetricImperial(2);
                            } else {
                                AolonFitManager.getInstance().setMetricImperial(1);
                            }
                        }
                    });
                }
                if (!deviceUnitDialog.isShowing()) {
                    deviceUnitDialog.show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (deviceUnitDialog != null) {
            deviceUnitDialog.dismiss();
            deviceUnitDialog.destroyDialog();
            deviceUnitDialog = null;
        }
        super.onDestroy();
    }
}
